<?php defined('SYSPATH') or die('No direct script access.');

/**
 * Twitxr Service Driver Library 
 * http://twitxr.com/public_api
 *
 * $Id$
 *
 * @package		Service Module
 * @author		Alex Sancho
 * @copyright	(c) 2008 Alex Sancho
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *	  notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *	  notice, this list of conditions and the following disclaimer in the
 *	  documentation and/or other materials provided with the distribution.
 * 3. Neither the name of copyright holders nor the names of its
 *	  contributors may be used to endorse or promote products derived
 *	  from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
 * PURPOSE ARE DISCLAIMED.	IN NO EVENT SHALL COPYRIGHT HOLDERS OR CONTRIBUTORS
 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 */

class Service_Twitxr_Driver extends Service_Driver 
{
	protected $api_url = "twitxr.com/api/rest";
	protected $user = null;
	protected $pass = null;

	/**
	 *	__construct
	 * 
	 * @param array $config
	 * @return void
	 * @access public
	 * 
	 */
	public function __construct($config = array())
	{
		$config += Kohana::config('service.twitxr');
		
		foreach($config as $key => $val)
			$this->$key = $val;
	}

	/**
	 * check_string_location
	 * 
	 * @param string $location
	 * @return object SimpleXMLElement Object
	 * @access public
	 * 
	 */
	public function check_string_location($location)
	{
		return $this->api_call('/checkStringLocation/?'.http_build_query(array('location' =>$location)));
	}

	/**
	 * get_user_data
	 * 
	 * @param string $user
	 * @return object SimpleXMLElement Object
	 * @access public
	 * 
	 */
	public function get_user_data($user) 
	{
		return $this->api_call('/getUserData/?'.http_build_query(array('user' => $user)));
	}

	/**
	 * post_update
	 * 
	 * @param string $image
	 * @param string $text
	 * @param string $source
	 * @param string $place
	 * @return object SimpleXMLElement Object
	 * @access public
	 * 
	 */
	public function post_update($image = false, $text = false, $source = '', $place = '')
	{
		if ( ! $image and ! $text)
			throw new Kohana_User_exception('Twitxr Driver', Kohana::lang('service.post_error'));
		
		$params = array
		(
			'text'		=> $text,
			'source'	=> $source,
			'place'		=> $place
		);
		
		if ($image and file_exists($image)) 
			$params['image'] = '@' . $image;
			
		return $this->post($params)->api_call('/postUpdate/');
	}

	/**
	 * get_user_timeline
	 * 
	 * @param string $user
	 * @param integer $since
	 * @param integer $page
	 * @return object SimpleXMLElement Object
	 * @access public
	 * 
	 */
	public function get_user_timeline($user, $since = '', $page = '')
	{
		$query = array
		(
			'user'	=> $user,
			'since' => $since,
			'page'	=> $page
		);

		return $this->api_call('/getUserTimeline/?'.http_build_query($query));
	}

	/**
	 * get_friends_timeline
	 * 
	 * @param string $user
	 * @param integer $since
	 * @param integer $page
	 * @return object SimpleXMLElement Object
	 * @access public
	 * 
	 */
	public function get_friends_timeline($user, $since = '', $page = '')
	{
		$query = array
		(
			'user'	=> $user,
			'since' => $since,
			'page'	=> $page
		);

		return $this->api_call('/getFriendsTimeline/?'.http_build_query($query));
	}

	/**
	 * get_public_timeline
	 * 
	 * @return object SimpleXMLElement Object
	 * @access public
	 * 
	 */
	public function get_public_timeline()
	{
		return $this->api_call('/getPublicTimeline/');
	}

	/**
	 * get_local_timeline
	 * 
	 * @param string $user
	 * @param string $place
	 * @return object SimpleXMLElement Object
	 * @access public
	 * 
	 */
	public function get_local_timeline($user, $place)
	{
		$query = array
		(
			'user'	=> $user,
			'place' => $place
		);

		return $this->api_call('/getLocalTimeline/?'.http_build_query($query));
	}

	/**
	 * get_user_replies
	 * 
	 * @param integer $since
	 * @param integer $page
	 * @return object SimpleXMLElement Object
	 * @access public
	 * 
	 */
	public function get_user_replies($since = '', $page = '')
	{
		$query = array
		(
			'since' => $since,
			'page'	=> $page
		);

		return $this->api_call('/getUserReplies/?'.http_build_query($query));
	}

	/**
	 * api_call
	 * 
	 * @param string $api_url
	 * @return object SimpleXMLElement Object
	 * @access protected
	 * 
	 */
	protected function api_call($api_url)
	{
		if ( ! $result = parent::api_call($api_url, false)) 
			throw new Kohana_User_Exception('Twitxr Driver', Kohana::lang('service.invalid_response', $this->last_api_call(), $this->last_status_code()));
	
		$xml = simplexml_load_string($result);
	
		if (false === $xml) 
			throw new Kohana_User_Exception('Twitxr Driver', Kohana::lang('service.xml_parse_error'));

		return $xml;
	}

	/**
	 * api_url
	 * 
	 * @param string $path
	 * @param string $separator
	 * @return string
	 * @access protected
	 * 
	 */
	protected function api_url($path, $separator = '&')
	{
		return $this->user . ':' . md5($this->pass) . '@' . $this->api_url . $path;
	}

} //End Twitxr Driver